package handles

import (
	"github.com/gin-gonic/gin"
	"image-text-mgr/models"
	"image-text-mgr/pkg"
	"strconv"
)

func Login(c *gin.Context) {

	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	var user models.User

	if user.Password != pkg.Md5(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var user []models.User

	models.DB.Offset(offset).Limit(size).Find(&user)
	var total int64
	models.DB.Model(&models.User{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "成功",
		"data":  user,
		"total": total,
	})
	return
}
